// main window
window.setTimeout((function(){
    //in iframe or opened win return
    if (self.location != top.location || self.opener) {
        return;
    }
    //no result return 
    if (!document.getElementById("pop-data")) {
        return;
    }
    //log
    function gm_log(s){
        try {
            console.log(s);
        } 
        catch (err) {
        }
    }
    //openWin
    function gm_openWin(){
        var ff = document.getElementById("fti").value;
        var tt = document.getElementById("qti").value;
        var sTitle = tt + "new~ " + ff + "-" + tt;
        window.setTimeout((function(){
            try {
                var winNewTicket = window.open("about:blank", "_" + searchKey, "width=400,height=260,toolbar=no,location=yes,directories=no,status=yes,resizable=yes,scrollbars=no");
                winNewTicket.document.write("<html><head><title>" + sTitle + "  New Ticket!</title></head><body style=\"background-color:#E8D8E2;margin:0px\"><div style=\"width=400px;height=260px;\"><div style='width:100%;float:left;height:100px;position:absolute'></div><embed width='400' height='260' flashvars='bannerSID=http://bm.alimama.cn/x/2010-01-29/11-51/2010-01-29_7e3f83f96e6df480d7aab3fd68fa537f_5.xml&amp;ds_sn=p4p_wt1&amp;ds_host=adData' allowscriptaccess='always' wmode='transparent' quality='high' name='wt' id='wt' style='' src='http://bm.alimama.cn/bcv1.swf' type='application/x-shockwave-flash'></embed></div></body></html>");
                winNewTicket.document.close();
            } 
            catch (err) {
                alert(decodeURIComponent("%E8%AF%B7%E7%82%B9%E5%87%BB%E7%AA%97%E5%8F%A3%E4%B8%8A%E6%96%B9%2C%E8%AE%BE%E7%BD%AE%E5%85%81%E8%AE%B8piao.kuxun.cn%E5%BC%B9%E5%87%BA%E7%AA%97%E5%8F%A3%2C%E4%BB%A5%E4%BE%BF%E5%9C%A8%E5%88%B7%E5%88%B0%E6%96%B0%E7%81%AB%E8%BD%A6%E7%A5%A8%E5%90%8E%E5%BE%97%E5%88%B0%E6%98%8E%E6%98%BE%E7%9A%84%E6%8F%90%E7%A4%BA!"));
            }
        }), 100);
    }
    //globalStorage
    function gm_storeTimeStamp(){
        gm_log("store last query time" + gPopupTimeStamp + "" + (new Date(gPopupTimeStamp * 1000)));
        gm_log("searchKey:" + searchKey);
        window.globalStorage["piao.kuxun.cn"][searchKey] = gPopupTimeStamp;
    }
    //searchKey
    var searchKey = encodeURIComponent(location.href.split("#")[0]);
    //gm_log("searchKey:"+searchKey);
    //timeStamp
    var timeStamp = window.globalStorage["piao.kuxun.cn"][searchKey];
    
    if (!timeStamp) {
        gm_log("first query");
        //first time
        timeStamp = gPopupTimeStamp;
        //todo render all require jquery
        try {
            $(".w1").prepend('<span><img src="http://sta.kuxun.cn/Files/piao/image/pop-new.gif"></span>');
        } 
        catch (err) {
        }
        //openWin
        gm_openWin();
    }
    else {
        //other times
        timeStamp = parseInt(timeStamp, 10);
        gm_log("last query time" + timeStamp + "" + (new Date(timeStamp * 1000)));
        //30min or lastcall;
        if (gPopupTimeStamp > timeStamp + 1800) {
            gm_log("over 30min since last query");
            gPopupTimeStamp = timeStamp + 1800;
        }
        else {
            gm_log("within 30min since last query");
            gPopupTimeStamp = timeStamp;
        }
    }
    //storeTimeStamp
    gm_storeTimeStamp();
    //remove new require jquery
    try {
        $(".w1 a").click(function(){
            var rd = $(this).prev("span").empty();
            if (rd.length && gTitleTimer&&gTitleOld) {
                window.clearInterval(gTitleTimer);
                document.title = gTitleOld;
            }
        });
    } 
    catch (err) {
    }
    
    //override
    window.onPopupResult = function(data){
        $.each(data.items, function(i, item){
            if (item.pubtime > gPopupTimeStamp) {
                gPopupTimeStamp = item.pubtime;
                gm_storeTimeStamp();
            }
        });
        if (data.items.length > 0) {
            gm_openWin();
            showPopupResult(data);
        }
        gPopupNum++;
        gPopupTimer = setTimeout(PopupResult, 30000 + Math.floor(20000 * (Math.random())));
    };
    
    //get in 
    try {
        window.clearTimeout(gPopupTimer);
    } 
    catch (err) {
    }
    try {
        gm_log("start query");
        PopupResult();
    } 
    catch (err) {
    }
    //stat
    var oimg = document.createElement("img");
    oimg.src = "http://img.tongji.linezing.com/1463439/tongji.gif";
    document.body.appendChild(oimg);
    //ad
    try{
    	$("#suggest").prepend('<a href="http://haibao.huoban.taobao.com/tms/topic.php?pid=mm_10000521_0_0&amp;eventid=101146" target="_blank"> <img width="200" src="http://img.alimama.cn/cms/images/1257347637156.jpg"></a>').append('<a href="http://haibao.huoban.taobao.com/tms/topic.php?pid=mm_10000521_0_0&amp;eventid=101145" target="_blank"> <img width="200" src="http://img.alimama.cn/cms/images/1257346970325.jpg"></a>');
    }catch(err){
    }
}), 5);

//for popup window
(function(){
    if (window.opener) {
        var ff = window.opener.document.getElementById("fti").value;
        var tt = window.opener.document.getElementById("qti").value;
        adData = [{
            "displayurl": "http:\/\/item.taobao.com\/auction\/item_detail.jhtml?item_id=0b7d55cc1db191041b40ce11991dd97f",
            "imgsrc": "http:\/\/img.alimama.cn\/cms\/images\/1263365621645.jpg",
            "title": ff,
            "price": 22000,
            "desc": "08\u65b0\u6b3e \u624b\u63d0\u5f0f\u6d3b\u6c27\u8db3\u6d74\u76c6 \u8db3\u6d74\u6309\u6469\u5668 2\u4e2a\u6708\u6d88\u9664\u6162\u6027\u75be\u75c5",
            "linkid": "ya0",
            
            "funcname": "_rQ"
        }, {
            "displayurl": "http:\/\/item.taobao.com\/auction\/item_detail.jhtml?item_id=94c4430d2589aaea959592845550df01",
            "imgsrc": "http:\/\/img04.taobaocdn.com\/tps\/i4\/T1H3lsXhVjXXXXXXXX-230-100.jpg",
            "title": tt,
            "price": 24800,
            "desc": " ",
            "linkid": "ya1",
            
            "funcname": "_rQ"
        },{
            "displayurl": "http:\/\/item.taobao.com\/auction\/item_detail.jhtml?item_id=0b7d55cc1db191041b40ce11991dd97f",
            "imgsrc": "http:\/\/img.alimama.cn\/cms\/images\/1263365621645.jpg",
            "title": "",
            "price": 22000,
            "desc": "08\u65b0\u6b3e \u624b\u63d0\u5f0f\u6d3b\u6c27\u8db3\u6d74\u76c6 \u8db3\u6d74\u6309\u6469\u5668 2\u4e2a\u6708\u6d88\u9664\u6162\u6027\u75be\u75c5",
            "linkid": "ya2",
            "clickurl": "http:\/\/pindao.huoban.taobao.com\/channel\/channelCode-10.htm?pid=mm_10000521_0_0",
            "funcname": "_rQ"
        }, {
            "displayurl": "http:\/\/item.taobao.com\/auction\/item_detail.jhtml?item_id=94c4430d2589aaea959592845550df01",
            "imgsrc": "http:\/\/img04.taobaocdn.com\/tps\/i4\/T1H3lsXhVjXXXXXXXX-230-100.jpg",
            "title": "",
            "price": 24800,
            "desc": " ",
            "linkid": "ya3",
            "clickurl": "http:\/\/s.click.taobao.com\/a\/qvMkqcoXHFE=-10000521&n=86",
            "funcname": "_rQ"
        }, {
            "displayurl": "http:\/\/item.taobao.com\/auction\/item_detail.jhtml?item_id=2e371efa84d0f109f856fd7628e4fa5a",
            "imgsrc": "http:\/\/img.alimama.cn\/cms\/images\/1264406646615.jpg",
            "title": "\u592e\u89c6\u70ed\u9500\uff0c\u7279\u4ef7\u62a2\u8d2d\uff0c\u7687\u5a01H-205C\u8db3\u6d74\u5668",
            "price": 35500,
            "desc": "\u8d85\u4f4e\u4ef7\u5b81\u6ce2\u7687\u5a01H-205C\u591a\u529f\u80fd\u8db3\u6d74\u5668 \u8db3\u6d74\u76c6 \u4e13\u67dc\u6b63\u54c1",
            "linkid": "ya4",
            "clickurl": "http:\/\/s.click.taobao.com\/a\/qvMkqCbcASY=-10000521&n=86",
            "funcname": "_rQ"
        }];
        window.taobaop4p_openToBlank = function(openUrl){
            var form, btn;
            if (!document.getElementById("itemClickForm")) {
                form = document.createElement('form');
                form.id = "itemClickForm";
                form.target = '_blank';
                form.method = 'post';
                form.style.display = 'none';
                btn = document.createElement('input');
                btn.type = 'submit';
                form.appendChild(btn);
                btn.id = "itemClickButton";
                document.body.appendChild(form);
                
            }
            else {
                form = document.getElementById("itemClickForm");
                btn = document.getElementById("itemClickButton");
            }
            
            form.action = openUrl;
            //alert(openUrl);
            btn.click();
        };
        window._rQ = function(u, o){
        		if(o=="ya0"||o=="ya0"){
        			return;
        		}
            taobaop4p_openToBlank(u);
        };
    }
})();

